<?php
/*
# $Id: Inflector.php 10 2006-11-18 12:34:32Z James $
#
# The Inflector provides a number of methods for converting given inputs into different outputs.
*/

class Inflector {

	/*
	# -------------------- GENERIC METHODS --------------------
	# These methods are generic conversions that are used by the more specific methods.
	# ---------------------------------------------------------
	*/

	/*
	# @method string lowerCamelCaps_upperCamelCaps( string $input )
	# $input	= String in lowerCamelCaps format (eg. lowerCamelCaps)
	#
	# Conversion: stringInputParam > StringInputParam
	*/
	static public function lowerCamelCaps_upperCamelCaps($input) {

		// Result
		return ucfirst($input);
	}

	/*
	# @method string upperCamelCaps_lowerCamelCaps( string $input )
	# $input	= String in UpperCamelCaps format (eg. UpperCamelCaps)
	#
	# Conversion: StringInputParam > stringInputParam
	*/
	static public function upperCamelCaps_lowerCamelCaps($input) {

		// Result
		$input[0] = strtolower($input[0]);
		return $input;
	}

	/*
	# @method string upperCamelCaps_lowerUnderscored( string $input )
	# $input	= String in Camlized format (eg. CamelizedWordInput)
	#
	# Conversion: StringInputParam > string_input_param
	*/
	static public function upperCamelCaps_lowerUnderscored($input) {

		// Convert and return
		return strtolower(preg_replace("/([a-z0-9])([A-Z])/", "$1_$2", $input));
	}

	/*
	# static string lowerUnderscored_upperCamelCaps( string $input )
	# $input	= String in lowercase and underscored format (eg. lower_undercased_word)
	#
	# Conversion: string_input_param > StringInputParam
	*/
	static public function lowerUnderscored_upperCamelCaps($input) {

		// Conversion cache
		static $convCache = array();

		// Convert, cache and return
		return isset($convCache[$input]) ? $convCache[$input] : $convCache[$input] = str_replace(' ', '', ucwords(str_replace('_', ' ', $input)));
	}

	/*
	# @method string lowerCamelCaps_lowerUnderscored( string $input )
	# $input	= String in studlyCaps format (eg. StudlyCapsWordInput)
	#
	# Conversion: stringInputParam > string_input_param
	*/
	static public function lowerCamelCaps_lowerUnderscored($input) {

		// Convert and return
		return strtolower(preg_replace("/([a-z0-9])([A-Z])/", "$1_$2", $input));
	}

	/*
	# static string lowerUnderscored_lowerCamelCaps( string $input )
	# $input	= String in lowercase and underscored format (eg. lower_undercased_word)
	#
	# Conversion: string_input_param > stringInputParam
	*/
	static public function lowerUnderscored_lowerCamelCaps($input) {

		// Convert, cache and return
		$output = str_replace(' ', '', ucwords(str_replace('_', ' ', $input)));
		$output[0] = strtolower($output[0]);
		return $output;
	}

	/*
	# @method string lowerHyphenated_upperCamelCaps( string $input )
	# $input	= String in format "string-input-param"
	#
	# Conversion: string-input-param > StringInputParam
	*/
	static public function lowerHyphenated_upperCamelCaps($input) {

		// Convert, cache and return
		return str_replace(' ', '', ucwords(str_replace('-', ' ', $input)));
	}

	/*
	# static string lowerHyphenated_lowerCamelCaps( string $input )
	# $input	= String in format "string-input-param"
	#
	# Conversion: string-input-param > stringInputParam
	*/
	static public function lowerHyphenated_lowerCamelCaps($input) {

		// Convert, cache and return
		$output = str_replace(' ', '', ucwords(str_replace('-', ' ', $input)));
		$output[0] = strtolower($output[0]);
		return $output;
	}

	/*
	# @method string upperCamelCaps_lowerHyphentated( string $input )
	# $input	= String in format "StringInputParam"
	#
	# Conversion: StringInputParam > string-input-param
	*/
	static public function upperCamelCaps_lowerHyphenated($input) {

		// Convert and return
		return strtolower(preg_replace("/([a-z0-9])([A-Z])/", "$1-$2", $input));
	}

	/*
	# @method string lowerCamelCaps_lowerHyphenated( string $input )
	# $input	= String in format "stringInputParam"
	#
	# Conversion: stringInputParam > string-input-param
	*/
	static public function lowerCamelCaps_lowerHyphenated($input) {

		// Convert and return
		return strtolower(preg_replace("/([a-z0-9])([A-Z])/", "$1-$2", $input));
	}

	/*
	# -------------------- SPECIFIC METHODS --------------------
	# These methods are used for specific conversions.
	# ----------------------------------------------------------
	*/

	/* -------------------- MODEL -------------------- */

	/*
	# @method string modelName_modelClass( string $modelName )
	# $modelName	= Model name in the camelized format, ie. CamelizedModelName
	#
	# Conversion: StringInputParam > StringInputParamModel
	*/
	static public function modelName_modelClass($modelName) {

		// Result
		return $modelName.'Model';
	}

	/*
	# @method string modelName_modelManagerClass( string $modelName )
	# $modelName	= Model name in the camelized format, ie. CamelizedModelName
	#
	# Conversion: StringInputParam > StringInputParamManager
	*/
	static public function modelName_modelManagerClass($modelName) {

		// Result
		return $modelName.'Manager';
	}

	/*
	# @method string modelName_dbTableName(( string $modelName )
	# $modelName	= Model name in the camelized format, ie. CamelizedModelName
	#
	# Conversion: StringInputParam > string_input_param
	*/
	static public function modelName_dbTableName($modelName) {

		// Result
		return self::upperCamelCaps_lowerUnderscored($modelName);
	}

	/*
	# @method string modelField_classMethod( string $fieldName )
	# $fieldName	= Model field name (ie. example_field_name)
	#
	# Conversion: string_input_param > StringInputParam
	*/
	static public function modelField_classMethod($fieldName) {

		// Conversion cache
		static $convCache = array();
		if(isset($convCache[$fieldName])) return $convCache[$fieldName];

		// Result
		return $convCache[$fieldName] = self::lowerUnderscored_upperCamelCaps($fieldName);
	}

	/*
	# @method string classMethod_modelField( string $classMethod )
	# $classMethod	= Class method (ie. MethodName)
	#
	# Conversion: StringInputParam > stringInputParam
	*/
	static public function classMethod_modelField($classMethod) {

		// Result
		return strtolower($classMethod[0]).substr($classMethod, 1);
	}

	/* -------------------- CONTROLLER -------------------- */

	/*
	# @method string controllerCommand_controllerClass( string $controllerCommand )
	# $controllerCommand	= Controller command as used in a URL command (ie. hyphenated-controller-name)
	#
	# Conversion: string-input-param > StringInputParamController
	*/
	static public function controllerCommand_controllerClass($controllerCommand) {

		// Result
		$controllerCommand = strtolower(preg_replace("/[^a-zA-Z0-9\-]/i", "-", $controllerCommand));
		return self::lowerHyphenated_upperCamelCaps($controllerCommand.'-controller');
	}

	/*
	# @method string controllerClass_controllerCommand( string $controllerClass )
	# $controllerClass	= Controller class name (ie. ExampleClassController)
	#
	# Conversion: StringInputParamController > string-input-param
	*/
	static public function controllerClass_controllerCommand($controllerClass) {

		// Result
		return self::upperCamelCaps_lowerHyphenated(preg_replace("/Controller$/", "", $controllerClass));
	}

	/*
	# @method string actionCommand_actionMethod( string $actionCommand )
	# $actionCommand	= Action command as used in the URL, ie. underscored-command-name
	#
	# Conversion: string-input-param > stringInputParam
	*/
	static public function actionCommand_actionMethod($actionCommand) {

		// Result
		$actionCommand = strtolower(preg_replace("/[^a-zA-Z0-9\-]/i", "-", $actionCommand));
		return self::lowerHyphenated_lowerCamelCaps($actionCommand);
	}

	/* -------------------- EXTENSION -------------------- */

	/*
	# @method string extensionName_extensionClass( string $extensionName )
	# $extensionName	= Extension name (ie the folder name, eg. extension-name)
	#
	# Conversion: string-input-param > StringInputParamExtension
	*/
	static public function extensionName_extensionClass($extensionName) {

		// Result
		return self::lowerHyphenated_upperCamelCaps($extensionName).'Extension';
	}
}
?>